package com.duk.controller;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

public class BaseFilter extends OncePerRequestFilter
{

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException
	{
		// 获取session里面的用户信息
		String u = (String) request.getSession().getAttribute("userInSession1");
		// 获取前台的请求路径的URI
		StringBuffer url = request.getRequestURL();
		// 判断是否是请求的登录页面
		if (url.indexOf("/login/login.do") != -1 || url.indexOf("/login/checkUser.do") != -1
				|| url.indexOf("/login/loginOut.do") != -1)
		{
			// 如果是请求登录页面直接放过
			doFilter(request, response, filterChain);

		}

		else
		{
			// 不是请求的登录页面,但是u为空,也就是用户没登录,所以没权限需要强制跳转到登录页面
			if (u == null)
			{
				response.reset();
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			} else
			// 已经登录了,放行
			{
				doFilter(request, response, filterChain);
			}

		}
	}
}
